package com.ljfiction.book.fragment.ranking;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ljfiction.book.R;
import com.ljfiction.book.activitys.MainActivity;
import com.ljfiction.book.fragment.BaseFragment;
import com.ljfiction.book.fragment.SearchFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Android on 2017/12/7.
 */

public class RankTowtatleFragment extends BaseFragment {

    @BindView(R.id.tVreader_ranking)
    TextView tVreaderRanking;
    @BindView(R.id.tVauthor_ranking)
    TextView tVauthorRanking;
    @BindView(R.id.towbang_ranking)
    LinearLayout towbangRanking;
    @BindView(R.id.small_towfragment)
    FrameLayout smallTowfragment;
    Unbinder unbinder;
    private int type;
    private Context context;
    private RankNotatleFragment mfragment,fragmentmoth, fragmentAll;
    private FragmentManager  fragmentManager;

    public static RankTowtatleFragment newInstance(Context context, int type) {
        RankTowtatleFragment newFragment = new RankTowtatleFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        newFragment.setArguments(bundle);
        return newFragment;

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.ranking_towtatle, container, false);
        Bundle args = getArguments();
        if (args != null) {
            this.context = getContext();
            this.type = args.getInt("type");
        }
        fragmentManager=getChildFragmentManager();
        fragmentmoth=RankNotatleFragment.newInstance(context,type,2);
        addFragment(fragmentManager.beginTransaction(), R.id.small_towfragment, fragmentmoth, false);
      /*  mfragment=fragmentmoth;
        transaction.add(R.id.small_towfragment, mfragment).commit();*/
        unbinder = ButterKnife.bind(this, view);
        return view;
    }



    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.tVreader_ranking, R.id.tVauthor_ranking})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tVreader_ranking:
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                hideFragments(transaction);
                if (fragmentmoth==null){
                    fragmentmoth=RankNotatleFragment.newInstance(context,type,2);
                    addFragment(transaction, R.id.small_towfragment, fragmentmoth, false);
                }else {
                    showFragment(transaction, R.id.small_towfragment, fragmentmoth, false);
                }
                tVreaderRanking.setBackgroundResource(R.drawable.mine_tab_left_true);
                tVreaderRanking.setTextColor(Color.parseColor("#ffffff"));
                tVauthorRanking.setTextColor(Color.parseColor("#333333"));
                tVauthorRanking.setBackgroundResource(R.drawable.mine_tab_right_false);

                break;
            case R.id.tVauthor_ranking:
                FragmentTransaction transaction1 = fragmentManager.beginTransaction();
                hideFragments(transaction1);
                if (fragmentAll==null){
                    fragmentAll=RankNotatleFragment.newInstance(context,type,3);
                    addFragment(transaction1, R.id.small_towfragment, fragmentAll, false);
                }else {
                    showFragment(transaction1, R.id.small_towfragment, fragmentAll, false);
                }
                tVreaderRanking.setBackgroundResource(R.drawable.mine_tab_left_false);
                tVauthorRanking.setBackgroundResource(R.drawable.mine_tab_right_true);
                tVreaderRanking.setTextColor(Color.parseColor("#333333"));
                tVauthorRanking.setTextColor(Color.parseColor("#ffffff"));
                break;
        }
    }
    private void addFragment(FragmentTransaction transaction, int id, BaseFragment frament, boolean b) {
        if (b)
            transaction.setCustomAnimations(R.anim.push_left_in, R.anim.push_left_out, R.anim.back_left_in, R.anim.back_right_out);
        transaction.add(id, frament);
        transaction.commit();
    }
    private void hideFragments(FragmentTransaction transaction) {
        hideFragment(transaction, fragmentAll);
        hideFragment(transaction, fragmentmoth);

    }
    private void showFragment(FragmentTransaction transaction, int id, BaseFragment frament, boolean b) {
        if (b)
            transaction.setCustomAnimations(R.anim.push_left_in, R.anim.push_left_out, R.anim.back_left_in, R.anim.back_right_out);
        transaction.show(frament);
        transaction.commit();
    }
    public void hideFragment(FragmentTransaction transaction, BaseFragment frament) {
        if (frament != null) {
            transaction.hide(frament);
        }
    }
}
